package com.zzh.jn_erp.user.controller;


import com.zzh.jn_erp.common.UnifyResult;
import com.zzh.jn_erp.user.entity.User;
import com.zzh.jn_erp.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 跳转登录页
     * @return
     */
    @GetMapping("/login")
    public ModelAndView toLoginWeb(){
        return new ModelAndView("/login/login");
    }

    /**
     * 登录
     * @param user 参数
     * @return
     */
    @PostMapping("doLogin")
    public UnifyResult doLogin(User user){
        if(user == null) {
            return UnifyResult.fail().message("参数异常");
        }
        if(StringUtils.isEmpty(user.getUserName())) {
            return UnifyResult.fail().message("登录人不可为空");
        }
        if(StringUtils.isEmpty(user.getUserPassword())) {
            return UnifyResult.fail().message("密码不可为空");
        }
        if(userService.validateUser(user)) {
            return UnifyResult.success();
        } else {
            return UnifyResult.fail();
        }
    }
}
